
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base href="<%=basePath%>">
<title>总部</title>
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/main.css">
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<script src="${pageContext.request.contextPath}/js/MD5.js"></script>
<script src="${pageContext.request.contextPath}/js/city2.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/citySelect2.js"></script>

</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="left.jsp"%>
	<style>
.main_div {
	min-width: 1000px;
	min-height: 60px
}

.force-overflow1-1 {
	border: none
}

.buttonPic>span {
	color: #FFFFFF;
	font-size: 36px
}

.countyVipCount {
	font-size: 18px;
	line-height: 50px
}

.countyVipCount span {
	color: red
}

.pageChange {
	float: right;
	color: #337ab7;
}

.pageChange>span {
	cursor: pointer
}

.pageChange>span:hover {
	color: blue
}
</style>
	<div id="index"
		style="position: absolute;left: 180px;top: 80px;bottom: 0;right: 0;">
		<section id="main2" style="display:none">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div">
					<div class="main_title">
						<span>代理商管理</span>
					</div>
					<form class="form-horizontal" style="margin-top: 60px">
						<div class="form-group" id="city_4">
							<label class="col-xs-2 control-label" style="text-align: right">省市区：</label>
							<div class="col-xs-1 control-label">
								<select class="form-control" id="prov4" data-code="510000"></select>
							</div>
							<div class="col-xs-1 control-label">
								<select class="form-control" id="city4" data-code="-1"></select>
							</div>
							<div class="col-xs-1 control-label">
								<select class="form-control" id="area4" data-code="-1"></select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">账号：</label>
							<div class="col-xs-3">
								<input type="text" id="name" class="form-control"
									placeholder="请输入用户名">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">密码：</label>
							<div class="col-xs-3">
								<input type="password"  id="userPassword1" class="form-control"
									placeholder="请输入密码">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">密码：</label>
							<div class="col-xs-3">
								<input type="password"  id="userPassword" class="form-control"
									placeholder="请再次输入密码">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">姓名：</label>
							<div class="col-xs-3">
								<input type="text" id="userName" class="form-control"
									placeholder="请输入代理商姓名">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">电话：</label>
							<div class="col-xs-3">
								<input type="text" id="userPhone" class="form-control"
									placeholder="请输入代理商电话">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label" style="text-align: right">身份证：</label>
							<div class="col-xs-3">
								<input type="text" id="addId" class="form-control"
									placeholder="请输入身份证">
							</div>
						</div>

						<script>
							var selectVal = new CitySelect({
								data : data,
								provId : "#prov4",
								cityId : '#city4',
								areaId : '#area4'
							});
						</script>
						<div class="form-group">
							<div class="col-xs-4 col-xs-offset-2">
								<button type="button" class="col-xs-6 btn btn-primary"
									onclick="submitPassword()">添加</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</section>
		<section id="main">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div">
					<div class="main_title">
						<span>代理商管理</span>
					</div>
				</div>
				<form class="form-inline"
					style="margin-top: 30px;margin-left: 40px">
					<div class="form-group" id="city_4">
						<label style="text-align: right">省市区：</label> <select
							class="form-control" id="prov42" data-code="510000"></select> <select
							class="form-control" id="city42" data-code="-1"></select> <select
							class="form-control" id="area42" data-code="-1"></select>
				    </div>					
					<div class="form-group">
						<label style="margin-left:30px">代理商状态：</label>
						<select class="form-control" id="status">
							<option value="0">请选择</option>
							<option value="1">正常运营</option>
							<option value="2">已冻结</option>
						</select>
					</div>
					<div class="form-group">
						<button type="button" onclick="selectinfo()"
							class="btn btn-primary">立即查询</button>
					</div>
					<div class="form-group" style="margin-left:300px">
							<button type="button" onclick="addNewAgentcss('#main','#main2')"
								class="btn btn-primary">新增代理商</button>						
					</div>
				</form>
				<script>
					var selectVal = new CitySelect({
						data : data,
						provId : "#prov42",
						cityId : '#city42',
						areaId : '#area42'
					});
				</script>
				<div class="force-overflow1-1">
					<table class="table table-condensed table-bordered table-hover"
						id="showTable" style="text-align: center">
						<tr style="background-color: #59c7d2">
							<th style="text-align: center">序号</th>
							<th style="text-align: center">运营商名称</th>
							<th style="text-align: center">运营商电话</th>
							<th style="text-align: center">运营商地址</th>
							<th style="text-align: center">运营商状态</th>
							<th style="text-align: center">运营商总营业额</th>
							<th style="text-align: center">操作</th>
						</tr>
					</table>
					<div class="pageChange">
						<span onclick="prePage()">&nbsp;&nbsp;上一页&nbsp;&nbsp;</span> <span
							onclick="nextPage()">&nbsp;&nbsp;下一页&nbsp;&nbsp;</span> <em>
							&nbsp; 第&nbsp;<span id="pageNum">1</span>&nbsp;页&nbsp;&nbsp;
						</em> <em>/&nbsp;&nbsp;共&nbsp;<span id="totalpage"></span>&nbsp;页
						</em> <em>&nbsp;&nbsp;&nbsp;跳到第<input id="searchInput"
							style="width:46px" maxlength="4" type="text">页
						</em>
						<button class="btn btn-primary" onclick="searchPage()">搜索</button>
					</div>
					<input style="display:none" value="7" id="pageCount"
						name="pageCount" />
				</div>
			</div>
		</section>
	</div>
	<script>
	
		function submitPassword() {
			var card = /^[a-z0-9A-Z_-]{6,20}$/;
			var phones=/^(1)[0-9]{10}$/;			
			var userPassword = $("#userPassword").val();
			if( $("#userPassword").val()!=$("#userPassword1").val()){
			 alert("两次密码输出不一致");
			 return;
			}
			if(!phones.test($("#userPhone").val())){
			alert("请输入正确的手机号");
			 return;
			}
			if(($("#area4").find("option:selected").val()==-1)||(!$("#name").val())||(!$("#userName").val())){
			alert("请填写完整信息");
			 return;
			}
			if (!card.test(userPassword)) {
				alert("请输入6到20位由数字、字母、下划线、减号组成的密码")
	
			} else {
			var agentName=$("#prov4").find("option:selected").text()+
			$("#city4").find("option:selected").text()+
			$("#area4").find("option:selected").text();
				$.post(urls + "agentAction/addAgentInfo",
					{
						agentName : agentName,
						agentRegion : $("#area4").find("option:selected").val(),
						phone : $("#userPhone").val(),
						password : $.md5(userPassword),
						principal : $("#userName").val(),
						account : $("#name").val(),
						addId:$("#addId").val()
					},
					function(data) {
						if (data==250) {
							alert("账号或代理商已存在");							
						} else if (data==false){
							alert("添加代理商失败")
						}else if (data==true){
							alert("添加代理商成功")
							addNewAgentcss("#main2", "#main")
							selectinfo();
						}
					})
			}
		}
		function addNewAgentcss(m, m2) {
			$(m).css("display", "none");
			$(m2).css("display", "block");
		}
	
	
		$(function() {
			selectinfo()
		})
	
		
	
		function selectinfo() {
			var pageNum = $("#pageNum").html();
			var pageCount = $("#pageCount").val();
			var status = $("#status").find("option:selected").val();
			var area = $("#area42").find("option:selected").val();
			if (status == 0) {
				status = null
			}
			if (area == -1) {
				area = null
			}
			$.post(urls + "agentAction/selectAgentInfoList",
				{
					pageNum : pageNum,
					pageCount : pageCount,
					status : status,
					agentRegion : area
				},
				function(data) {
					//console.log(data)
					if (data) {
						var totalpage = data.totalpage;
						var orderlist = data.list;
						var table = $("#showTable");
						$("#vipCount").html(data.totalVip)
						$("tr").remove(".addtr");
						for (var i = 0; i < orderlist.length; i++) {
							var button;
							var status;
							var button1 = "<button class='btn btn-success' onclick='controlAgent(\"" + orderlist[i].id + "\",1)'>恢复代理商</button>";
							var button2 = "<button class='btn btn-primary' onclick='controlAgent(\"" + orderlist[i].id + "\",2)'>冻结代理商</button>";
							if (orderlist[i].status == 1) {
								button = button2;
								status = "正常营业中"
							} else {
								button = button1;
								status = "已冻结"
							}
							var append = "<tr class='addtr'><td>" + (i + 1) + "</td>" +
								"<td >" + orderlist[i].principal + "</td>" +
								"<td>" + orderlist[i].phone + "</td>" +
								"<td>" + orderlist[i].agentName + "</td>" +
								"<td>" + status + "</td>" +
								"<td>" + orderlist[i].totalmoney + "</td>" +
								"<td>" + button + "</td></tr>";
							table.append(append);
						}
						$("#totalpage").html(totalpage);
					}
				}
				, "json"
			)
		}
		function controlAgent(id, status) {
			
			if(status==1){
			if(confirm("点击确定:更换代理商,点击取消:恢复原代理商？")){
			location.href="${pageContext.request.contextPath}/YMGback/head/updateAgent.jsp?agentId="+id;
			}else{
						$.post(urls + "agentAction/changeStatusAgentInfo",
					{
						id : id,
						status : status
					},
					function(res) {
						if (res) {
							selectinfo()
						} else {
							alert("操作失败")
						}
					}
				)
			}
			}else{
			if (confirm("是否继续操作？")) {
			$.post(urls + "agentAction/changeStatusAgentInfo",
					{
						id : id,
						status : status
					},
					function(res) {
						if (res) {
							selectinfo()
						} else {
							alert("操作失败")
						}
					}
				)
			}
				
			}
		}
	</script>
</body>
</html>